package com.shu.refrence;

import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2021/7/27 20:36
 */

public class PhantomRefTest {

    public static void main(String[] args) {
        Object counter = new Object();
        ReferenceQueue refQueue = new ReferenceQueue<>();
        PhantomReference<Object> p = new PhantomReference<>(counter, refQueue);
        counter = null;
        System.gc();

        PhantomRefTest.class.getClassLoader();


        new Thread(()->{
            System.out.println("0000");
        }).start();


        new Thread(()->{
            System.out.println("1111");
        }).start();

        try {
            // Remove是一个阻塞方法，可以指定timeout，或者选择一直阻塞
            Reference<Object> ref = refQueue.remove(1000L);
            if (ref != null) {
                // do something
                System.out.println("do something");
            }
        } catch (InterruptedException e) {
            // Handle it
            System.out.println("Handle it");
        }
    }
}
